package com.tech.zhidengxian.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tech.zhidengxian.model.constant.PurchaseType;
import com.tech.zhidengxian.model.dataobject.GoodsDO;
import com.tech.zhidengxian.model.dataobject.MessageDO;
import com.tech.zhidengxian.model.dataobject.PurchaseDO;
import com.tech.zhidengxian.model.dto.page.MyPage;
import com.tech.zhidengxian.model.dto.request.PurchaseRequest;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * @author Ljh
 * @date 2023/5/11 1:28
 * @description 求购模块
 **/
@Mapper
public interface PurchaseMapper extends BaseMapper<PurchaseDO> {
    default IPage<PurchaseDO> pageQuery(PurchaseRequest purchaseRequest){
        Page<PurchaseDO> page = new Page<>(purchaseRequest.getCurrentPage(), purchaseRequest.getPageSize());
        LambdaQueryWrapper<PurchaseDO> wrapper = new LambdaQueryWrapper<>();
        PurchaseType type = purchaseRequest.getPurchaseType();
        if (PurchaseType.hot.equals(type)) {
            wrapper.eq(PurchaseDO::getSchool, purchaseRequest.getContent())
                    .orderByDesc(PurchaseDO::getBrowseCount)
                    .orderByDesc(PurchaseDO::getCommentCount);
        }else if (PurchaseType.newest.equals(type)) {
            wrapper.eq(PurchaseDO::getSchool, purchaseRequest.getContent())
                    .orderByDesc(PurchaseDO::getCreateTime);
        }else if (PurchaseType.local.equals(type)) {
            wrapper.eq(PurchaseDO::getCity, purchaseRequest.getContent())
                    .orderByDesc(PurchaseDO::getBrowseCount)
                    .orderByDesc(PurchaseDO::getCommentCount)
                    .orderByDesc(PurchaseDO::getCreateTime);
        }else {
            wrapper.like(PurchaseDO::getContent, purchaseRequest.getContent())
                    .orderByDesc(PurchaseDO::getBrowseCount)
                    .orderByDesc(PurchaseDO::getCommentCount)
                    .orderByDesc(PurchaseDO::getCreateTime);
        }
        return selectPage(page, wrapper);
    }
}
